package emergence.game.gfx;

public class Colors {

    public static int get(int color1, int color2, int color3, int color4) {
        return (get(color4) << 24) + (get(color3) << 16) + (get(color2) << 8) + get(color1);
    }

    private static int get(int color) {
        if (color < 0) {
            return 255;
        }

        int r = color / 100 % 10;
        int g = color / 10 % 10;
        int b = color % 10;

        return r * 36 + g * 6 + b;
    }
    
    public static int getColorAtIndex(int color, int index) {
        switch(index) {
            case 1:
                return reverseGet(color & 0xFF);
            case 2:
                return reverseGet((color >> 8) & 0xFF);
            case 3:
                return reverseGet((color >> 16) & 0xFF);
            case 4:
                return reverseGet((color >> 24) & 0xFF);
            default:
                return -5; // Change to something more sensible at some point
        }
    }
    
    private static int reverseGet(int color) {
        if (color >= 255) {
            return -1;
        }
        
        int b = color % 6;
        int g = (color / 6) % 6;
        int r = (color / 36) % 6;
        
        return (r * 100) + (g * 10) + b;
    }
}
